package com.briup.corejava.day19;

import com.briup.corejava.day10.pojo.Student;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 将Student对象写到文件里进行保存
 * ObjectOutputStream
 * FileOutputStream
 */
public class Test6_ow {
    public static void main(String[] args) {
        ObjectOutputStream oos = null;
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream("src/com/briup/corejava/day19/file/object.txt");
            oos = new ObjectOutputStream(fos);

            Student s1 = new Student("张三",20);
            Student s2 = new Student("李四",22);
            Student s3 = new Student("王武",19);
            // 多个数据考虑使用集合存取
            List<Student> list = new ArrayList<>();
            Collections.addAll(list,s1,s2,s3);
            oos.writeObject(list);


        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {

        }
    }
}
